package com.deyuanyun.pic.settings.service.pipe.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationQueryVO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipePositionDTO;
import com.deyuanyun.pic.settings.domain.pipe.PipeLine;
import com.deyuanyun.pic.settings.domain.pipe.PipeSegment;
import com.deyuanyun.pic.settings.mapper.pipe.PipeLocationMapper;
import com.deyuanyun.pic.settings.mapper.pipe.PipeSegmentMapper;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;

@Service
public class PipeLocationServiceImpl implements PipeLocationService {

    private @Resource PipeLocationMapper pipeLocationMapper;
    private @Resource PipeSegmentMapper pipeSegmentMapper;//管段

    //如果再加一个参数，就干脆变成直接传递对象吧。
    @Override
    public List<PipeLocationDTO> queryPipeLocation(String pipeLineId,
            BigDecimal beginMileage, BigDecimal endMileage,String signName,String targetType) {
        
        PipeLocationQueryVO pipeLocationQuery = new PipeLocationQueryVO();
        pipeLocationQuery.setPipeLineId(pipeLineId);
        pipeLocationQuery.setBeginMileage(beginMileage);
        pipeLocationQuery.setEndMileage(endMileage);
        pipeLocationQuery.setSignName(signName);
        pipeLocationQuery.setTargetType(targetType);
        List<PipeLocationDTO> list = pipeLocationMapper.queryPipeLocation(pipeLocationQuery);
        return list;
    }

    
    @Cacheable("tempStoreData")
    @Override
    public PipeLocationDTO queryPipeLocationById(String pipeLineId,String id) {

        PipeLocationQueryVO pipeLocationQuery = new PipeLocationQueryVO();
        pipeLocationQuery.setId(id);
        pipeLocationQuery.setPipeLineId(pipeLineId);
        List<PipeLocationDTO> list = pipeLocationMapper.queryPipeLocation(pipeLocationQuery);
        if(list == null || list.isEmpty()){
            return null;
        }else{
            return list.get(0);
        }
        
    }
    
    @Cacheable("tempStoreData")
    @Override
    public PipeLocationDTO queryPipeLocationByName(String pipeLineId,String signName){
        PipeLocationQueryVO pipeLocationQuery = new PipeLocationQueryVO();
        pipeLocationQuery.setPipeLineId(pipeLineId);
        pipeLocationQuery.setSignName(signName);
        List<PipeLocationDTO> list = pipeLocationMapper.queryPipeLocation(pipeLocationQuery);
        if(list == null || list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
    
     

    public PipePositionDTO queryPipeLinePosition(String pipeLineId){
        //GISID被删除了
        PipeLine pipeLine = PipeDictUtils.getLineById(pipeLineId);
        String beginId = pipeLine.getFromPipeValveroomId();
        String endId = pipeLine.getToPipeValveroomId();
        
        //Asserts.assertTrue(beginId != null && StringUtils.isNotEmpty(beginId), "异常数据：管道起点不能为空、" + pipeLine.getName());
        //Asserts.assertTrue(endId != null && StringUtils.isNotEmpty(endId), "异常数据：管道终点不能为空、" + pipeLine.getName());
        PipePositionDTO pipeSegmentPosition = new PipePositionDTO();
        if(beginId != null && StringUtils.isNotEmpty(beginId)){
            PipeLocationDTO beginPipeLocation = queryPipeLocationById(pipeLineId,beginId);
            Asserts.assertTrue(beginPipeLocation != null, "异常数据：没有找到起点位置的绝对距离。" );
            
            pipeSegmentPosition.setBeginId(beginId);
            pipeSegmentPosition.setBeginName(beginPipeLocation.getName());
            pipeSegmentPosition.setBeginType(beginPipeLocation.getType());
            pipeSegmentPosition.setBeginMileage(beginPipeLocation.getMileage());
            pipeSegmentPosition.setBeginMileage2(beginPipeLocation.getMileage2());
            pipeSegmentPosition.setBeginBallMileage(beginPipeLocation.getValveRoomBallMileage());
            pipeSegmentPosition.setBeginBallMileage2(beginPipeLocation.getValveRoomBallMileage2());
            
        }
        if(endId != null && StringUtils.isNotEmpty(endId)){
            PipeLocationDTO endPipeLocation = queryPipeLocationById(pipeLineId,endId);
            Asserts.assertTrue(endPipeLocation != null, "异常数据：没有找到终点位置的绝对距离。");
            
            pipeSegmentPosition.setEndId(endId);
            pipeSegmentPosition.setEndName(endPipeLocation.getName());
            pipeSegmentPosition.setBeginType(endPipeLocation.getType());
            pipeSegmentPosition.setEndMileage(endPipeLocation.getMileage());
            pipeSegmentPosition.setEndMileage2(endPipeLocation.getMileage2());
            pipeSegmentPosition.setEndBallMileage(endPipeLocation.getValveRoomBallMileage());
            pipeSegmentPosition.setEndBallMileage2(endPipeLocation.getValveRoomBallMileage2());
            
        }
        
        pipeSegmentPosition.setId(pipeLine.getId());
        pipeSegmentPosition.setName(pipeLine.getName());
        
        return pipeSegmentPosition;
    }

    
    public PipePositionDTO queryPipeSegmentPosition(String pipeSegmentId){
        //GISID被删除了
        PipeSegment pipeSegment = pipeSegmentMapper.selectByPrimaryKey(pipeSegmentId);
        String beginId = pipeSegment.getFromPipeValveroomId();
        String endId = pipeSegment.getToPipeValveroomId();
        String pipeLine = pipeSegment.getPipeLineId();
        
        PipePositionDTO pipeSegmentPosition = new PipePositionDTO();
        //Asserts.assertTrue(beginId != null && StringUtils.isNotEmpty(beginId), "异常数据：管段起点不能为空、" + pipeSegment.getName());
        //Asserts.assertTrue(endId != null && StringUtils.isNotEmpty(endId), "异常数据：管段起点不能为空、" + pipeSegment.getName());
        if(beginId != null && StringUtils.isNotEmpty(beginId)){

            PipeLocationDTO beginPipeLocation = queryPipeLocationById(pipeLine,beginId);
            Asserts.assertTrue(beginPipeLocation != null, "异常数据：没有找到起点位置的绝对距离。");
            
            pipeSegmentPosition.setBeginId(beginId);
            pipeSegmentPosition.setBeginName(beginPipeLocation.getName());
            pipeSegmentPosition.setBeginType(beginPipeLocation.getType());
            pipeSegmentPosition.setBeginMileage(beginPipeLocation.getMileage());
            pipeSegmentPosition.setBeginMileage2(beginPipeLocation.getMileage2());
            pipeSegmentPosition.setBeginBallMileage(beginPipeLocation.getValveRoomBallMileage());
            pipeSegmentPosition.setBeginBallMileage2(beginPipeLocation.getValveRoomBallMileage2());
        }
        
        if(endId != null && StringUtils.isNotEmpty(endId)){

            PipeLocationDTO endPipeLocation = queryPipeLocationById(pipeLine,endId);
            Asserts.assertTrue(endPipeLocation != null, "异常数据：没有找到终点位置的绝对距离。");
            pipeSegmentPosition.setEndId(endId);
            pipeSegmentPosition.setEndName(endPipeLocation.getName());
            pipeSegmentPosition.setBeginType(endPipeLocation.getType());
            pipeSegmentPosition.setEndMileage(endPipeLocation.getMileage());
            pipeSegmentPosition.setEndMileage2(endPipeLocation.getMileage2());
            pipeSegmentPosition.setEndBallMileage(endPipeLocation.getValveRoomBallMileage());
            pipeSegmentPosition.setEndBallMileage2(endPipeLocation.getValveRoomBallMileage2());
        }

        pipeSegmentPosition.setId(pipeSegment.getId());
        pipeSegmentPosition.setName(pipeSegment.getName());
        
        return pipeSegmentPosition;
    }

}
